//
//  PictureTableCell.m
//  MiaoTu
//
//  Created by dllo on 16/9/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PictureTableCell.h"
#import "Masonry.h"
#import "PictureCollectionCell.h"
#import "Photo.h"
#import "UIView+Frame.h"

#define kPictureCollectionCell @"PictureCollectionCellIdentifier"

@interface PictureTableCell () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation PictureTableCell

- (void)setPicArray:(NSArray *)picArray {
    _picArray = picArray;
    [_collectionView reloadData];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
        fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
        [self.contentView addSubview:_collectionView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.contentView).offset(10);
        make.bottom.right.mas_equalTo(self.contentView).offset(-10);
    }];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"PictureCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kPictureCollectionCell];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPictureCollectionCell forIndexPath:indexPath];
    cell.picString = _picArray[indexPath.item].Url;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    [self.contentView layoutIfNeeded];
    return CGSizeMake(self.collectionView.dk_height, self.collectionView.dk_height);
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
